package com.itheima.sys.controller;

import com.itheima.common.vo.PageResult;
import com.itheima.common.vo.Result;
import com.itheima.sys.dto.CompanyUserDTO;
import com.itheima.sys.dto.DepartmentDTO;
import com.itheima.sys.dto.DepartmentOrUserSimpleDTO;
import com.itheima.sys.dto.MemberExcelRequest;
import com.itheima.sys.service.ICompanyUserService;
import com.itheima.sys.service.OrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
public class OrganizationController {

    @Autowired
    private ICompanyUserService companyUserService;

    /**
     * 分页获取部门成员列表
     * @param page 当前页
     * @param pageSize 页大小
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/organization/members/simple")
    public Result<PageResult<CompanyUserDTO>> queryCompanyMembersByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize
    ) throws Exception {
        return Result.success("查询成功", companyUserService.queryCompanyMembersByPage(page, pageSize));
    }


    @Autowired
    private OrganizationService organizationService;


    /**
     * 获得组织架(部门)构树数据
     * @return
     */
    @GetMapping("/organization/department")
    public Result<List<DepartmentDTO>> queryDeparentTree() {
        return Result.success(organizationService.queryDeparentTree());
    }

    /**
     * 分页获取部门成员列表
     * @param page 当前页
     * @param pageSize 页大小
     * @param departmentId 部门ID
     * @param keyword 查询关键字
     * @throws Exception
     */
    @GetMapping(value = "/organization/members")
    public Result<PageResult<CompanyUserDTO>> queryCompanyMembers(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
            @RequestParam(value = "departmentId", required = false) Long departmentId,
            @RequestParam(value = "keyword", required = false) String keyword) throws Exception {
        return Result.success(organizationService.queryCompanyMembers(page, pageSize, departmentId, keyword));
    }

    /**
     * PC:通讯录Json导入
     * @param memberExcelChineseDTOs 中文key excel表格数据
     */
    @PostMapping(value = "/organization/members/list")
    public Result uploadExcel(@RequestBody MemberExcelRequest memberExcelChineseDTOs) throws Exception {
        organizationService.excelImport(memberExcelChineseDTOs);
        return Result.success();
    }

    /**
     * PC:接收上传Excel文件，导入企业用户
     * @param file
     * @throws Exception
     */
    @PostMapping(value = "/organization/members/uploadXls", consumes = "multipart/*", headers = "content-type=multipart/form-data")
    public void uploadExcel(@RequestParam("file") MultipartFile file) throws Exception {
        organizationService.parseExcel(file);
    }


    /**
     * 查询企业部门ID
     * @return
     */
    @GetMapping("/organization/department/top")
    public Result<List<Long>> queryDepartmentIds() {
        return Result.success(organizationService.queryDepartmentIds());
    }

    /**
     * PC:获取部门简单列表：根据部门ID，查询该部门下员工
     * @param departmentId 上级部门ID
     * @param includeMember 是否包含部门成员 //0为包含，1为不包含  默认为包含
     * @return 部门列表
     * @throws Exception
     */
    @GetMapping(value = "/organization/simple")
    public Result<List<DepartmentOrUserSimpleDTO>> querySimpleDepartment(
            @RequestParam(value = "id", required = false) Long departmentId,
            @RequestParam(value = "includeMember", required = false) Integer includeMember
    ) throws Exception {
        return Result.success(organizationService.querySimpleDepartment(departmentId, includeMember));
    }
}
